package com.testing.vip12splogin.controller;

import com.mysql.cj.Session;
import com.testing.vip12splogin.model.Result;
import com.testing.vip12splogin.model.User;
import com.testing.vip12splogin.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Classname UserController
 * @Description 类型说明
 * @Date 2022/5/22 22:07
 * @Created by 特斯汀Roy
 */
@RestController
@RequestMapping(value = "/user", produces = "application/json")
@Api(tags = "用户类操作")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ApiOperation("登录接口")
    public Result login(
            String username,
            String password, HttpServletRequest request, HttpServletResponse response) {
        //从请求中获取本次会话session。
        HttpSession session = request.getSession();
        //唯一标识本次的session信息的id号。
        String sessionId = session.getId();
        System.out.println("sessionid是"+sessionId);
        //设置session生命周期是120
        session.setMaxInactiveInterval(120);
        //获取cookie的信息
        Cookie[] cookies = request.getCookies();
        //用之前先判空
        if(cookies!=null){
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }
        //首先尝试获取session中的loginName,没登录，获取到的就是null
        Object loginName = session.getAttribute("loginName");
        if(loginName!=null){
            //如果已经登录了，则返回不能再次登录
            if(loginName.toString().equals(username)){
                //如果已经登录过的用户存储在session中的用户名和当前输入用户名一致
                return new Result(-100,"不能再次登录",
                        "您"+loginName+"已经登录过了");
            }else{
                return new Result(-101,"不能再次登录",
                        "已经有另一个用户"+loginName+"登录过了");
            }
        }

        //进行用户名密码的认证
        Result login = userService.login(username, password);
        if(login.getCode()==200) {
            //可以在session中存储想要的信息。
            session.setAttribute("loginName", username);
            //通过重新返回sessionid的cookie来完成生命周期重置
            Cookie jsessionidCookie=new Cookie("JSESSIONID",sessionId);
            jsessionidCookie.setMaxAge(120);
            //注意，需要设置path和原本的cookie是一样的用/
            jsessionidCookie.setPath("/");
            //将cookie通过返回给客户端。
            response.addCookie(jsessionidCookie);

        }
        return login;
    }

    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        System.out.println("登出接口中的sessionId是"+sessionId);
        //将session置为失效的。
        session.invalidate();
        return new Result(200,"登出成功","不管有没有用户，反正都帮你注销了");
    }

    @PostMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        Object loginName = request.getSession().getAttribute("loginName");
        return userService.getUser(loginName.toString());
    }

    @PutMapping("/register")
    public Result register(@RequestBody User user){
        return userService.register(user.getUsername(),user.getPassword(),user.getNickname(),user.getDescribe());
    }

    @DeleteMapping("/delete/{username}")
    public Result delUser(@PathVariable String username){
        return userService.deleteUser(username);
    }


}
